/**
 *	@author Juntalis
 */

/**
 * A workaround to Adobe AIR's security in regards to creating files inside the app's folder.
 * @param {air.File} scriptpath		A file object pointed at where you want the file generated.
 * @param {String} scriptcontents	The contents of the proposed file.
 * @param {Function} callback		A function() to call when we are done.
 */
function CreateFile(scriptpath, scriptcontents, callback)
{
	// Create file objects.
	var stFile = new air.FileStream();
	var tempJs =  air.File.createTempFile();
	
	// Write new script file.
	try {						
		stFile.open(tempJs, air.FileMode.WRITE);
			stFile.writeUTFBytes(scriptcontents);
		stFile.close();			
	} catch(error) {
		if (tempJs.exists) tempJs.deleteFile();
	    alert("Error while writing temporary file!\n" + error);
		if(typeof(callback)!="undefined")
			callback();
		return;
	}
	
	// If native process is supported, automate the user bot installation.
	if (air.NativeProcess.isSupported) {
		try {
			// Create temporary shell script.
			var osSystem = air.Capabilities.os;
			var tmpShellScript = air.File.createTempFile();
			var tmpShScrExec = new air.File();
			var tmpShScrStartInfo = new air.NativeProcessStartupInfo();
			var tmpShScrArgs = new air.Vector["<String>"]();
			var tmpProc = new air.NativeProcess();
			var tmpShScrContents = "";
			
			// Create file according to operating system.
			if (osSystem.match(/Windows/mi)) {
				// Windows
				tmpShellScript.nativePath = tmpShellScript.nativePath + ".bat";
				tmpShScrContents = "@ECHO OFF\n"+
				"MOVE /Y \"" + tempJs.nativePath + "\" \"" + scriptpath.nativePath + "\"";
				tmpShScrExec.nativePath = "C:\\Windows\\system32\\cmd.exe";
				tmpShScrArgs.push("/C");
				tmpShScrArgs.push(tmpShellScript.nativePath);
			} else if (osSystem.match(/(Mac OS)|(Linux)/mi)) {
				// Mac OS X and Linux both use unix-style shell scripts.
				tmpShellScript.nativePath = tmpShellScript.nativePath + ".sh";
				tmpShScrContents = "#!/bin/bash\n"+
				"mv -f \"" + tempJs.nativePath + "\" \"" + scriptpath.nativePath + "\"";
				tmpShScrExec.nativePath = "/bin/sh";
				tmpShScrArgs.push(tmpShellScript.nativePath);
				
			} else {
				// Not sure. So instead we'll do what we would've if the
				// system couldn't do native processes.
				alert("I am unsure of your operating system, and was unable to dynamically generate the file necessary. Sorry.");
				if(tempJs.exists) tempJs.deleteFile();
				if(typeof(callback)!="undefined")
					callback();
				return;
			}
			
			// Use the os's line breaks.
			tmpShScrContents = tmpShScrContents.replace(/\n/g, air.File.lineEnding);
			stFile.open(tmpShellScript, air.FileMode.WRITE);
			stFile.writeUTFBytes(tmpShScrContents);
			stFile.close();
			
			// Launch application
            tmpShScrStartInfo.executable = tmpShScrExec;
            tmpShScrStartInfo.arguments = tmpShScrArgs;
            tmpProc.start(tmpShScrStartInfo);
            tmpProc.addEventListener(air.NativeProcessExitEvent.EXIT,
				function(event) {
					// Finished, now we do our last acts.
					if (tmpShellScript.exists) tmpShellScript.deleteFile();
					
					// Callback
					if(typeof(callback)!="undefined")
						callback();
				}
			);						
		} catch (error) {
			if (tmpShellScript.exists) tmpShellScript.deleteFile();
			alert("There was an error while automating the generation of your file.\n\nError Details\n" + error);
			if(tempJs.exists) tempJs.deleteFile();
			// Callback
			if(typeof(callback)!="undefined")
				callback();
		}
	} else {
		alert("Unfortunately, your system is unable to execute processes on the fly. Because of this, I was unable to fufill your request.");
		if(tempJs.exists) tempJs.deleteFile();
		if(typeof(callback)!="undefined") callback();
	}
}